package com.example.leetcode.trainingcamp.week6.Thursday;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * 从上到下打印出二叉树的每个节点，同一层的节点按照从左到右的顺序打印。
 */
public class Offer32_I {

    public int[] levelOrder(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        List<Integer> list = new ArrayList<>();
        if (root == null) return new int[0];
        queue.add(root);
        while (queue.size() != 0){
            TreeNode poll = queue.poll();
            list.add(poll.val);
            if (poll.left != null) queue.add(poll.left);
            if (poll.right != null) queue.add(poll.right);
        }
        int[] result = new int[list.size()];
        for (int i =0;i<list.size();i++){
            result[i] = list.get(i);
        }
        return result;
    }

}
